﻿using MeetingRoomBooking.Application.Common.Interfaces;
using MeetingRoomBooking.Domain.Events;

namespace MeetingRoomBooking.Application.Rooms.Commands;
public class DeleteBookingCommand : IRequest
{
    public int Id { get; set; }
}

public class DeleteBookingCommandHander : IRequestHandler<DeleteBookingCommand>
{
    private readonly IApplicationDbContext _context;  

    public DeleteBookingCommandHander(IApplicationDbContext context)
    {
        _context = context;     
    }    

    public async Task Handle(DeleteBookingCommand request, CancellationToken cancellationToken)
    {
        var booking = await _context.Bookings.FindAsync(request.Id);
       
        if (booking == null) throw new Exception($"Booking Id:{request.Id} not found.");

        _context.Bookings.Remove(booking);       

        await _context.SaveChangesAsync(cancellationToken);          
    }
}
